package com.um.jdy.platform.order.controller;

import com.um.jdy.common.charge.entity.po.*;
import com.um.jdy.common.charge.entity.vo.*;
import com.um.jdy.platform.order.entity.dto.*;
import com.um.jdy.platform.order.entity.vo.ComplaintOrderItemVO;
import com.um.jdy.platform.order.entity.vo.OrderDetailVO;
import com.um.jdy.platform.order.entity.vo.OrderItemVO;
import com.um.jdy.platform.order.manager.ChargeOrderManager;
import com.um.springboot.starter.annotation.ApiPermission;
import com.um.springboot.starter.annotation.RateLimiter;
import com.um.springboot.starter.annotation.TokenRequired;
import com.um.springboot.starter.entity.Page;
import com.um.springboot.starter.orm.controller.BaseController;
import com.um.springboot.starter.orm.entity.Response;
import com.um.springboot.starter.orm.params.ExportParams;
import com.um.springboot.starter.orm.params.PageParams;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 充电订单
 *
 * @author cs
 * 日期 2023/6/7
 * 版权所有 广州优匠科技有限公司
 */
@RestController
@RequestMapping("/api/v1/cms/order")
@Api(tags = "充电订单")
@ApiPermission("charge")
@TokenRequired
@Validated
public class ChargeOrderController extends BaseController<ChargeOrderManager> {

    /**
     * 分页查询订单
     *
     * @param params
     * @return
     */
    @ApiPermission("page")
    @ApiOperation("分页查询订单")
    @PostMapping("page")
    public Response<Page<OrderItemVO>> page(@RequestBody ChargeOrderPageDTO params) {
        PageParams pageParams = params.toPageParams(
                new String[]{"deviceStationId","orderNo","outTradeNo","nickname","mobile","status","payStatus",
                        "isException","exceptionStatus","returnStatus","isFree","deviceType","deviceCode"},
                new String[]{"o.device_station_id","o.order_no","o.out_trade_no","u.nickname","u.mobile","o.status","o.pay_status",
                        "o.is_exception","o.exception_status","o.return_status","o.is_free","o.device_type","o.device_code"},
                "o.create_time","beginTime","endTime");
        return manager.page(pageParams);
    }

    /**
     * 投诉订单分页查询
     *
     * @param params
     * @return
     */
    @ApiPermission("complaint_page")
    @ApiOperation("分页查询投诉订单")
    @PostMapping("complaint_page")
    public Response<Page<ComplaintOrderItemVO>> getComplaintPage(@RequestBody ComplaintOrderPageDTO params) {
        PageParams pageParams = params.toPageParams(
                new String[]{"deviceStationId","orderNo","nickname","mobile","complaintStatus","deviceType","deviceCode"},
                new String[]{"o.device_station_id","o.order_no","u.nickname","u.mobile","c.complaint_status","o.device_type","o.device_code"},
                "o.create_time","beginTime","endTime");
        return manager.getComplaintPage(pageParams);
    }

    /**
     * 修改投诉订单状态
     * @param id
     * @param status
     * @return
     */
    @ApiPermission("update_complaint")
    @ApiOperation("修改投诉订单状态")
    @PutMapping("/{id}/complaint_status/{status}")
    public Response changeComplaintStatus(@PathVariable String id,@PathVariable String status){
        return manager.changeComplaintStatus(id,status);
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @ApiPermission("get")
    @ApiOperation("详情")
    @GetMapping({"{id}"})
    public Response<OrderDetailVO> get(@PathVariable String id) {
        return manager.get(id);
    }

    /**
     * 充电订单退款失败，后台重新退款
     *
     * @param refundDTO
     * @return
     */
    @ApiOperation("重新退款")
    @PostMapping("refund")
    public Response refund(@RequestBody OrderNoDTO refundDTO) {
        return manager.refund(refundDTO);
    }

    /**
     * 充电订单指定金额退款
     *
     * @param refundMoneyDTO
     * @return
     */
    @ApiPermission("refund")
    @ApiOperation("指定金额退款")
    @PostMapping("refund_money")
    public Response refundMoney(@Valid @RequestBody RefundMoneyDTO refundMoneyDTO) {
        return manager.refundMoney(refundMoneyDTO);
    }

    /**
     * 停止充电
     *
     * @param orderNoDTO
     * @return
     */
    @ApiPermission("stop")
    @ApiOperation("停止充电")
    @PostMapping("stop")
    public Response stop(@Valid @RequestBody OrderNoDTO orderNoDTO) {
        return manager.stop(orderNoDTO);
    }

    /**
     * 订单日志
     *
     * @param orderIdDTO
     * @return
     */
    @ApiPermission("log")
    @ApiOperation("订单日志")
    @PostMapping("get_logs")
    public Response<List<DataLog>> getLogs(@RequestBody OrderIdDTO orderIdDTO) {
        return manager.getLogs(orderIdDTO);
    }


    /**
     * 查询订单实时功率
     * @param orderIdDTO
     * @return
     */
    @ApiPermission("powers")
    @ApiOperation("查询订单实时功率")
    @PostMapping("get_order_powers")
    public Response<List<OrderPower>> getOrderPowers(@RequestBody OrderIdDTO orderIdDTO) {
        return manager.getOrderPowers(orderIdDTO);
    }

    @ApiPermission("handle_exception")
    @ApiOperation(value = "异常订单标为已处理")
    @PostMapping("handle_exception")
    public Response handleExceptionOrder(@RequestBody List<String> list) {
        return manager.handleExceptionOrder(list);
    }

    @ApiPermission("handle_complaint")
    @ApiOperation(value = "投诉订单标为已处理")
    @PostMapping("handle_complaint")
    public Response handleComplaintOrder(@RequestBody List<String> list) {
        return manager.handleComplaintOrder(list);
    }

    @ApiPermission("order_bill")
    @ApiOperation(value = "查询充电订单账单")
    @GetMapping("order_bill")
    public Response<OrderBillVO> getOrderBill(String charge_order_id) {
        return Response.success(manager.getOrderBill(charge_order_id));
    }

    @ApiPermission("order_fee")
    @ApiOperation(value = "查询充电订单费用")
    @GetMapping("order_fee")
    public Response<List<OrderFeeVO>> getOrderFee(String charge_order_id) {
        return Response.success(manager.getOrderFee(charge_order_id));
    }

    @ApiPermission("place_fee")
    @ApiOperation(value = "查询充电订单占位费")
    @GetMapping("order_place_fee")
    public Response<List<OrderPlaceFeeVO>> getOrderPlaceFee(String charge_order_id) {
        return Response.success(manager.getOrderPlaceFee(charge_order_id));
    }

    @ApiPermission("bms_power")
    @ApiOperation(value = "查询充电BMS功率")
    @GetMapping("bms_power")
    public Response<List<EvBmsPowerVO>> getBmsPower(@ApiParam("订单ID") String charge_order_id) {
        return Response.success(manager.getBmsPower(charge_order_id));
    }

    @ApiPermission("order_energy")
    @ApiOperation(value = "查询单车充电时段明细")
    @GetMapping("order_energy")
    public Response<List<OrderEnergyItem>> getOrderFeeList(String charge_order_id) {
        return Response.success(manager.getOrderFeeList(charge_order_id));
    }

    /**
     * 导出充电订单
     *
     * @param params
     * @return
     */
    @ApiPermission("export")
    @ApiOperation(value = "导出充电订单")
    @PostMapping("/export_order_page")
    public Response exportOrderPage(@RequestBody ChargeOrderExportDTO params) {
        ExportParams exportParams = params.toExportParams(
                new String[]{"deviceStationId","orderNo","outTradeNo","nickname","mobile","status","payStatus",
                        "isException","exceptionStatus","returnStatus","isFree","deviceType","deviceCode"},
                new String[]{"o.device_station_id","o.order_no","o.out_trade_no","u.nickname","u.mobile","o.status","o.pay_status",
                        "o.is_exception","o.exception_status","o.return_status","o.is_free","o.device_type","o.device_code"},
                "o.create_time","beginTime","endTime");
        return manager.exportOrderPage(exportParams);
    }

    /**
     * 导出投诉订单
     *
     * @param params
     * @return
     */
    @ApiPermission("export_complaint")
    @ApiOperation(value = "导出投诉订单")
    @PostMapping("/export_complaint_order_page")
    public Response exportComplaintOrderPage(@RequestBody ChargeOrderExportDTO params) {
        ExportParams exportParams = params.toExportParams(
                new String[]{"deviceStationId","orderNo","nickname","mobile","complaintStatus","deviceType","deviceCode"},
                new String[]{"o.device_station_id","o.order_no","u.nickname","u.mobile","c.complaint_status","o.device_type","o.device_code"},
                "o.create_time","beginTime","endTime");
        return manager.exportComplaintOrderPage(exportParams);
    }

}
